const validate = RxFormValidator.generate([
    RxElemValidator.limitLen('idcId', '[IDC/ISP 许可证号] 长度最大为 128 位', 128),
    RxElemValidator.limitLen('operName', '[经营者名称] 长度最大为 128 位', 128),
    RxElemValidator.limitLen('companyCorporation', '[企业法人] 长度最大为 30 位', 30),
])


/**
 * @author rxliuli
 */
const app = new BasicTableVue({
    data() {
        return {
            api: baseOperCompanyApi,
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({prop: 'idcId', title: '经营许可证号', customComponent: true}),
                new TableColumn({prop: 'operName', title: '经营者名称'}),
                new TableColumn({
                    prop: 'companyCorporation',
                    title: '企业法人',
                }),
                new TableColumn({prop: 'securityOwnerName', title: '安全责任人'}),
                new TableColumn({prop: 'emergencyContactName', title: '应急联系人'}),
                new TableColumn({prop: 'count', title: '总数', customComponent: true}),
                new TableColumn({
                    prop: 'reportStatus',
                    title: '报备阶段',
                    formatter: fieldFormatter.facade(fieldFormatter.baseData.reportStatusMap),
                }),
                new TableColumn({prop: 'updateTime', title: '更新时间', formatter: TableColumn.datetimeFormat}),
                new TableColumn({prop: 'operating', title: '操作', customComponent: true}),
            ],
            validate,
        }
    },
    methods: {
        createForm() {
            return new BaseOperCompany()
        },
        viewInfo(row) {
            forward('/basic-data/see/admin_idc_info_see', row)
        },
        updateInfo(row) {
            forward('/basic-data/modify/admin_idc_info_modify', row)
        },
        selection: function (data) {
            this.selectedIdList = data.map(({idcId}) => idcId)
            this.selectedList = data
        },
        async insertReport(id) {
            if (await ajaxOption({
                res: this.api.insertReport(id),
                danger: '新增上报失败',
                success: '新增上报成功',
            })) {
                await this.searchPage()
            }
        },
        async downloadTemplate() {
            await baseOperCompanyApi.downloadTemplate('经营单位信息管理导入模板.xlsx')
        },
        async updateReport(id) {
            if (await ajaxOption({
                res: this.api.updateReport(id),
                danger: '修改上报失败',
                success: '修改上报成功',
            })) {
                await this.searchPage()
            }
        },
        checkSelectedReport() {
            const map = rx.arrayToMap(this.page.records, ({idcId}) => idcId, ({reportStatus}) => reportStatus)
            // 如果正在上报中则禁止
            if (this.selectedList.every(({idcId}) => map.get(idcId) !== 1)) {
                return true
            }
            rxPrompt.warningMsg('不能删除正在上报中的数据')
            return false
        },
        async init() {
        },
    },
})
